@model IAsyncEnumerable<Post>
@inject IOptionsSnapshot<BlogSettings> settings
@{
    int currentPage = int.Parse(ViewContext.RouteData.Values[Constants.page] as string ?? "0");

    int totalPosts;
    totalPosts = int.TryParse(ViewData[Constants.TotalPostCount]!.ToString(), out totalPosts) ? totalPosts : 0;

    int totalPages = (totalPosts / this.settings.Value.PostsPerPage) - (totalPosts % this.settings.Value.PostsPerPage == 0 ? 1 : 0);
}

@await foreach (var post in Model)
{
    await Html.RenderPartialAsync("Post.cshtml", post);
}

<nav class="pagination container" aria-label="Pagination">
    @if ((await Model.AnyAsync()) && currentPage < totalPages)
    {
        <a rel="prev" href="@ViewData[Constants.prev]" title="Older posts">&laquo; Older</a>
    }
    @if (currentPage > 0)
    {
        <a rel="next" href="@ViewData[Constants.next]" title="Newer posts">Newer &raquo;</a>
    }
    <br /><br />

    @section Head {
        @if ((await Model.AnyAsync()) && currentPage < totalPages)
        {
            <link rel="prev" href="@ViewData[Constants.prev]" />
        }
        @if (currentPage > 0)
        {
            <link rel="next" href="@ViewData[Constants.next]" />
        }
    }
</nav>

@{await Html.RenderPartialAsync("_SocialSharing", ViewData[Constants.Title]);}
